<template>
    <div>
        <el-dialog title="注册" :visible="registerShow" width="38%" @close="dialogClose">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <div class="content">
                    <el-form-item label="姓名" prop="userName">
                        <el-input v-model="ruleForm.userName" placeholder=""></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" prop="phone">
                        <el-input v-model="ruleForm.phone" placeholder=""></el-input>
                    </el-form-item>
                    <!-- 验证码 -->
                    <el-form-item label="验证码" prop="smsCode">
                        <div style="display:flex;justify-content:space-between">
                            <el-input v-model="ruleForm.smsCode" placeholder="请输入6位验证码" maxlength="6" show-word-limit
                                class="input_width" style="width:70%;"></el-input>
                            <el-button v-bind:type="button_sms_type" v-on:click="sendSmsCode"
                                v-bind:disabled="button_sms_disable">
                                {{button_sms_value}}</el-button>
                        </div>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item style="text-align:center;" prop="agree">
                        <el-radio v-model="ruleForm.agree" label="同意" class="mini">我已阅读并同意
                            <el-button class="mini" type="text" @click="agreement">《用户协议》、
                                《隐私政策》<br>《免责条款》</el-button>
                        </el-radio>
                        <el-button class="btn" type="primary" @click="submitForm()">注册</el-button>
                    </el-form-item>
                    <el-form-item>
                        <div style="display:flex;justify-content:space-between;margin-top: 15px;">
                            <el-button class="mini" type="text" @click="dialogClose">返回登录</el-button>
                            <el-button class="mini" type="text" @click="link">联系我们</el-button>
                        </div>
                    </el-form-item>
                </div>
            </el-form>
        </el-dialog>
        <Link ref="linkRef" v-if="linkShow" :linkShow="linkShow" @dialogClose="linkClose">
        </Link>
        <Agreement ref="agreeRef" v-if="agreeShow" :agreeShow="agreeShow" @dialogClose="agreeClose"></Agreement>
    </div>
</template>

<script>
import Link from './link.vue';
import Agreement from './agreement.vue';
import {
    regist,
}from "@/api/regist"
export default {
    //接受父组件
    props: {
        registerShow: {
            type: Boolean
        }
    },
    data () {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            }
            else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            }
            else if (value !== this.ruleForm.password) {
                callback(new Error("两次输入密码不一致!"));
            }
            else {
                callback();
            }
        };
        return {
            button_sms_type: "success",
            button_sms_value: "验证码",
            button_sms_disable: false,
            sms_time_count: 60,
            sms_timer: null,
            linkShow: false,
            agreeShow: false,
            ruleForm: {},
            rules: {
                tel: [{
                    required: true,
                    message: "请输入手机号",
                    trigger: "blur"
                }],
                smsCode: [{
                    required: true,
                    message: "请输入验证码",
                    trigger: "blur"
                }],
                password: [
                    { validator: validatePass, trigger: "blur" }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: "blur" }
                ],
                agree: [{
                    required: true,
                    message: "请阅读协议并同意",
                    trigger: "change,blur"
                }]
            },
        };
    },
    methods: {
        link () {
            this.linkShow = true;
        },
        linkClose () {
            this.linkShow = false;
        },
        agreement () {
            this.agreeShow = true;
        },
        agreeClose () {
            this.agreeShow = false;
        },
        dialogClose () {
            this.$emit("dialogClose");
        },
        submitForm () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    regist(this.ruleForm).then((res) => {
                        alert("submit!");
                        this.$emit("dialogClose");
                    } )
                }
                else {
                    return;
                }
            });
        },
    },
    components: { Link, Agreement }
}
</script>

<style lang="less" scoped>
.btn {
    width: 50%;
    height: 36px;
    font-size: 16px;
}

.mini {
    font-size: 13px;
    color: #606266;
    text-decoration: underline
}

:deep .el-form-item--medium .el-form-item__label {
    line-height: 30px;
}

// .el-form {

// }
</style>